package cn.tedu.hongbeifang.service.impl;

import cn.tedu.hongbeifang.ex.ServiceException;
import cn.tedu.hongbeifang.mapper.CommentMapper;
import cn.tedu.hongbeifang.pojo.dto.CommentAddNewDTO;
import cn.tedu.hongbeifang.pojo.entity.Comment;
import cn.tedu.hongbeifang.pojo.vo.CommentIndexVO;
import cn.tedu.hongbeifang.pojo.vo.CommentStandVO;
import cn.tedu.hongbeifang.service.ICommentService;
import cn.tedu.hongbeifang.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @Auther:fzd
 * @Date:2023/3/23
 */
@Service
@Slf4j
public class CommentServiceImpl implements ICommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public void insert(CommentAddNewDTO commentAddNewDTO) {
        log.debug("开始处理添加评论的业务,参数是:{}",commentAddNewDTO);
        //由于评论有极大概率重复,所以,我们不对评论是否存在检查
        Comment comment = new Comment();

        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime parse = LocalDateTime.parse(format, DateTimeFormatter.ofPattern(format));
        comment.setCreated(parse);

        BeanUtils.copyProperties(commentAddNewDTO,comment);
        int insert = commentMapper.insert(comment);
        if(insert!=1){
            String message="服务器正忙,请稍后再试!";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public List<CommentIndexVO> select() {
        List<CommentIndexVO> select = commentMapper.select();
        return select;
    }

    @Override
    public List<CommentIndexVO> selectByInfoId(Long infoId) {
        //int counts = commentMapper.countByUserId(InfoId);
        List<CommentIndexVO> commentIndexVOS = commentMapper.selectByInfoId(infoId);
        return commentIndexVOS;
    }

    @Override
    //用户删除评论
    public void deleteByUserId(Long id,Long userId) {
        CommentStandVO commentStandVO = commentMapper.selectById(id);
        if(commentStandVO.getUserId()!=userId){
            return;
        }
        int count = commentMapper.deleteById(id);
        if(count!=1){
            String message="服务器正忙,请稍后再试!";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    //管理员删除某一条评论
    public void deleteById(Long id) {
        CommentStandVO commentStandVO = commentMapper.selectById(id);
        if(commentStandVO==null){
            // 是：数据不存在，抛出异常
            String message = "删除评论失败，尝试删除的评论数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int i = commentMapper.deleteById(id);
        if(i!=1){
            String message="服务器正忙,请稍后再试!";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    //管理员删除一个类别的所有评论
    public void deleteByInfoId(Long infoId) {
        //首先判断这个评论是否存在
        List<CommentIndexVO> commentIndexVOS = commentMapper.selectByInfoId(infoId);
        if(commentIndexVOS==null){
            // 是：数据不存在，抛出异常
            String message = "删除评论失败，尝试删除的资讯数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int count = commentMapper.deleteByInfoId(infoId);
        if(count<=0){
            String message="服务器正忙,请稍后再试!";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }


}
